from selenium.webdriver.common.by import By
from logUtil import my_log
from selenium.webdriver.support.wait import WebDriverWait


# 定义基础类
class Action:
    def __init__(self, driver):
        self.driver = driver
        self.log = my_log("BasePage")


    # 定义操作方法
    def by_xpath_click(self, value):
        """xpath点击操作"""
        self.by_find_element(By.XPATH, value).click()

    def by_id_click(self, value):
        """id点击操作"""
        self.by_find_element(By.ID, value).click()

    def by_id_send_keys(self, value, send_value):
        """通过ID定位元素并发送内容"""
        self.by_find_element(By.ID, value).send_keys(send_value)

    # webDriverWait
    def by_find_element(self, by, value, timeout=30, poll=2):
        """
        隐式等待, 寻找元素
        """
        WebDriverWait(self.driver, timeout, poll).until(lambda x:x.find_element(by, value))
        return self.driver.find_element(by, value)

    def get_toast(self, text, timeout=30, poll=2):
        """
        1. 使用by_find_element寻找元素, 类型xpath, value自定义
        2. WebDriverWait获取信息,
        """
        toast_log = "//*[contains(@text, '{}')]".format(text)
        ele = WebDriverWait(self.driver, timeout, poll).until(lambda x: x.find_element_by_xpath(toast_log))
        return ele.text
